﻿"""
本脚本仅在某服务器刚装完Ubuntu24后，初次启用时调用
mkdir tmp_for_start_docker_nginx && \
    git clone https://gitee.com/children1987/start_docker_nginx_ubuntu.git tmp_for_start_docker_nginx/t && \
    python3 ./tmp_for_start_docker_nginx/t/start_nginx.py
"""

import os
import shutil
import subprocess


def cmd(cmd_str):
    print(cmd_str)
    if subprocess.call(cmd_str, shell=True):
        raise Exception("{} 执行失败".format(cmd_str))


def main():
    if os.geteuid() != 0:
        raise Exception("请以root权限运行")

    print("安装 docker 开始")
    cmd('''curl -fsSL https://test.docker.com -o test-docker.sh && \
        sudo sh test-docker.sh''')
    print("安装 docker 完成")

    nginx_path = '/workspace/nginx'
    os.makedirs(nginx_path)

    shutil.move('./tmp_for_start_docker_nginx/t/nginx_base.conf', nginx_path)
    shutil.move('./tmp_for_start_docker_nginx/t/uwsgi_params', nginx_path)

    cmd('docker run --name nginx --restart always --network host -d -v /workspace/nginx/nginx_base.conf:/etc/nginx/nginx.conf -v /workspace:/workspace nginx:1.19.2-alpine')

    print("清理 tmp_for_start_docker_nginx 开始")
    shutil.rmtree('./tmp_for_start_docker_nginx', ignore_errors=True) 
    print("清理 tmp_for_start_docker_nginx 完成")


if __name__ == '__main__':
    main()
